import openpyxl
import os
from core import settings

class UseExcel:
    def __init__(self, excel_path, excel_filename):
        self.excel_path = excel_path
        self.excel_filename = excel_filename

    def read_excel(self):
        """获取表名"""
        books = openpyxl.load_workbook(self.excel_path)
        """获取工作表名"""
        sh = books[self.excel_filename]
        """将工作表名设置为列表"""
        res = list(sh.rows)
        """获取表头信息"""
        title = [i.value for i in res[0]]
        """获取其他信息"""
        cases = []
        for item in res[1:]:
            data = [i.value for i in item]
            idc = dict(zip(title, data))
            cases.append(idc)
        return cases

    """修改excel"""
    def write_excel(self, rows, column, value):
        """获取表名"""
        books = openpyxl.load_workbook(self.excel_path)
        """获取工作表名"""
        sh = books[self.excel_filename]
        """写入内容"""
        sh.cell(row=rows, column=column, value=value)
        """保存数据"""
        books.save(self.excel_path)


